package com.logos.cartoon.ui;

import com.formdev.flatlaf.FlatDarkLaf;
import com.logos.cartoon.service.UserService;
import jakarta.annotation.Resource;
import net.miginfocom.swing.MigLayout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author rose
 */
@Component
public class LoginFrame extends JFrame {

    private final UserService userService;
    private JTextField userText;
    private JPasswordField passwordField;

    private  HomeFrame homeFrame;

    @Autowired
    public void setHomeFrame(HomeFrame homeFrame) {
        this.homeFrame = homeFrame;
    }


    public LoginFrame(UserService userService) {
        this.userService = userService;
        initUI();
    }

    private void initUI() {
        setTitle("登录");
        setSize(800, 600);
        // 居中显示
        setLocationRelativeTo(null);

        // 使用 MigLayout 进行布局
        JPanel panel = new JPanel(new MigLayout("fill", "[][]", "[][][]"));

//        JPanel panel = new JPanel(new MigLayout("fillx, insets 50", "[][grow]", "[][][]10[]"));

        // 用户名标签和输入框
        JLabel userLabel = new JLabel("用户名:");
        userText = new JTextField(20);

        panel.add(userLabel, "gapright 10");
        panel.add(userText, "wrap");

        // 密码标签和输入框
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField(20);

        panel.add(passwordLabel, "gapright 10");
        panel.add(passwordField, "wrap");

        // 登录和注册按钮
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");

        panel.add(loginButton, "split 2, center");
        panel.add(registerButton, "center");

        // 设置 FlatLaf 主题
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        SwingUtilities.updateComponentTreeUI(this);

        // 添加背景图片
        ImageIcon backgroundImage = new ImageIcon("path/to/your/background.png");
        JLabel backgroundLabel = new JLabel(backgroundImage);
        getLayeredPane().add(backgroundLabel, Integer.MIN_VALUE);

        // 自定义组件样式
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        userLabel.setFont(font);
        passwordLabel.setFont(font);
        userText.setFont(font);
        passwordField.setFont(font);
        loginButton.setFont(font);
        registerButton.setFont(font);

        // 将面板添加到窗口
        add(panel);

        // 添加按钮点击事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passwordField.getPassword());

                if (userService.login(username, password)) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "登录成功！");
                    dispose(); // 关闭登录窗口
                    // 打开首页
                    SwingUtilities.invokeLater(() -> {
                        homeFrame.setVisible(true); // 假设你通过构造器注入了 HomeFrame
                    });
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到注册界面
                new RegisterFrame().setVisible(true);
            }
        });

        setVisible(true);
    }
}
